require("../util.js");
require("../component/components.js");
require("../component/selfmediaflow.js");
require("../component/selfmediaslide.js");
require("../../jsMt/util.js");
var vm = new Vue({
    el:"#selfMedia",
    data:{
        indexs:"1",
        loadFlag:false,
        garouselList:{},
        isClassify:false,
        loginUid:"",
        userCategoryList:{},  //分类信息
        hotNewsList:{},//新闻
        topList:{},//top10列表
        pageNumber:"1",
        latitude:"",//维度
        longitude:"",//经度
        hotAdvList:{},
        hotAdvList1:[],
        hotAdvList2:[],//人气列表
        allCategoryList:{},//所有分类信息
        circlesList:{},//圈子列表
        nearbyAdvList:{},
        nearbyAdvList1:[],
        nearbyAdvList2:[]//附近列表
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.loginUid=this.searchObject&&this.searchObject.loginUid;
        // this.data.uid=this.searchObject&&this.searchObject.uid;
        // this.id=this.searchObject&&this.searchObject.id;
        this.getCarouselList();
        this.getHotNewsList();
        this.getUserCategoryList();
        this.getTop10List();
        this.getHotAdvList();
        this.AllCategoryList();
        this.getCirclesList();
        this.getNearbyAdvList();
        lazyload();
    },
    methods:{
        //后退
        getBack:function(){
            history.go(-1);
        },
        bgimg:function(url){
            url = url?url:"/imgs/see/index/index-bgimg1.png";
            return {
                "background-image":'url("'+url+'"),url("/imgs/see/index/index-bgimg1.png")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //顶部轮播图
        getCarouselList:function(){
            var that=this;
            this.ajax({url:"/selfMedia/getCarouselList",dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.garouselList=b.json.data;
                    lazyload();
                }else{
                    vm.$refs.prompt.toast("获取轮播失败！")
                }
            });
        },
        //获取头条新闻信息
        getHotNewsList:function(){
            var that=this;
            this.ajax({url:"/selfMedia/getHotNewsList",dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.hotNewsList=b.json.data;
                    notice($(".hotNewsList"));
                }else{
                    vm.$refs.prompt.toast("获取新闻失败！")
                }
            });
        },
        //获取自媒体主页top10列表
        getTop10List:function(){
            var that=this;
            this.ajax({url:"/selfMedia/getTop10List",dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.topList=b.json.data;
                    notice($(".hotNewsList"));
                }else{
                    vm.$refs.prompt.toast("获取top10列表失败！")
                }
            });
        },
        //通过当前登录用户id 获取分类信息
        getUserCategoryList:function(){
            var that=this;
            this.ajax({url:"/selfMedia/userCategoryList",data:{loginUid:that.loginUid},dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.userCategoryList=b.json.data.sort(function(a,b){
                        return a.imgRank-b.imgRank});
                }else{
                    vm.$refs.prompt.toast("获取分类失败！")
                }
            });
        },
        //获取所有自媒体分类信息
        AllCategoryList:function(){
            var that=this;
            this.ajax({url:"/selfMedia/AllCategoryList",dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.allCategoryList=b.json.data.sort(function(a,b){
                        return a.imgRank-b.imgRank});
                    //console.log(that.allCategoryList)
                }else{
                    vm.$refs.prompt.toast("获取所有分类失败！")
                }
            });
        },
        //获取自媒体人气分页信息
        getHotAdvList:function(){
            var that=this;
            this.ajax({url:"/selfMedia/getHotAdvList",data:{pageNumber:that.pageNumber},dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.hotAdvList=b.json.data;
                    that.hotAdvList1=that.hotAdvList.list;
                    if(that.hotAdvList.pageNumber>1){
                        if(that.hotAdvList.list.length>0) {
                            that.hotAdvList2 = that.hotAdvList2.concat(b.json.data.list);
                        }
                    }else{
                        that.hotAdvList2= that.hotAdvList1;
                    }
                    vm.loadFlag=false;
                    //lazyload();
                }else{
                    that.hotAdvList={};
                    that.hotAdvList1=[];
                    that.hotAdvList2=[];
                    vm.$refs.prompt.toast("获取人气列表失败！")
                }
                //console.log(that.hotAdvList)
            });

        },
        //获取主页圈子列表
        getCirclesList:function(){
            var that=this;
            this.ajax({url:"/selfMedia/getCirclesList",dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.circlesList=b.json.data.sort(function(a,b){
                        return a.imgRank-b.imgRank});
                    //console.log(that.allCategoryList)
                }else{
                    vm.$refs.prompt.toast("获取所有分类失败！")
                }
            });
        },
        //获取自媒体主页附近分页信息
        getNearbyAdvList:function(){
            var that=this;
            var data={
                pageNumber:that.pageNumber,
                latitude:"29.92",
                longitude:"116.63"
            };
            this.ajax({url:"/selfMedia/getNearbyAdvList",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.nearbyAdvList=b.json.data;
                    that.nearbyAdvList1=that.nearbyAdvList.list;
                    if(that.nearbyAdvList.pageNumber>1){
                        if(that.nearbyAdvList.list.length>0) {
                            that.nearbyAdvList2 = that.nearbyAdvList2.concat(b.json.data.list);
                        }
                    }else{
                        that.nearbyAdvList2= that.nearbyAdvList1;
                    }
                    vm.loadFlag=false;
                    console.log(that.nearbyAdvList)
                }else{
                    vm.$refs.prompt.toast("获取附近列表失败！")
                }
            });
        },
        //分类显示
        getIsClassify:function(){
            this.isClassify=!this.isClassify;
        },
        //人气切换
        centTitleTab:function(index){
            this.indexs=index;
            this.pageNumber=1;
            console.log(this.indexs)
        },
        //跳转页面
        getNext:function(id,name){
            location.href="/see/selfMediaPage?categoryId="+id+"&name="+encodeURI(name);
        }
    }
    ,watch:{
        hotAdvList2:function(){
            this.$nextTick(function(){
                console.log($(".selfMedia-flow").height());
                fmasonry();
                $("img.lazy").lazyload({
                    failurelimit:10,
                    load:fmasonry
                });
            });
        }
    }

});
function fmasonry() {
    $(".selfMedia-flow").masonry({
        // gutterWidth: 20,
        itemSelector: '.selfMedia-flowList',
        isAnimated: true
    });
}
// vm.$nextTick(function(){
//     setTimeout(function () {
//         fmasonry();
//         $("img.lazy").lazyload({
//             failurelimit:10,
//             load:fmasonry
//         });
//         var itemsNodeList = document.querySelectorAll(".selfMedia-flowList");
//         console.log(itemsNodeList)
//     }, 1000);
//     var itemsNodeList = document.querySelectorAll(".selfMedia-flowList");
//     console.log(itemsNodeList)
//
// });
//文档高度
function getDocumentTop() {
    var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;    return scrollTop;
}
//可视窗口高度
function getWindowHeight() {
    var windowHeight = 0;    if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
    } else {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}
//滚动条滚动高度
function getScrollHeight() {
    var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
    if (document.body) {
        bodyScrollHeight = document.body.scrollHeight;
    }
    if (document.documentElement) {
        documentScrollHeight = document.documentElement.scrollHeight;
    }
    scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
    return scrollHeight;

}
window.onscroll=function(){
    var loadFlag = vm.hotAdvList.lastPage;
    //监听事件内容
    if(getScrollHeight()==getWindowHeight() + getDocumentTop()){
        //当滚动条到底时,这里是触发内容
        if(!loadFlag){
            vm.loadFlag=true;
            vm.pageNumber++;
            vm.getHotAdvList();
        }else{
            vm.$refs.prompt.toast("没有更多了！")
        }
    }
};
//新闻滚动
function notice(ul){
    // var pText = ul.find("p").eq(0).html();
    // console.log(pText)
    // ul.append("<p>"+pText+"</p>");
    var num = 0;
    setInterval(function(){
        num ++;
        if(num == ul.find("p").length){
            num = 1;
            ul.css({
                marginTop:0
            });
        }
       ul.animate({
            marginTop:-20 * num
        },200);
    },3000);
}
function lazyload(){
    $("img.lazy").lazyload();
}